﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using YookingBizWQW.Class.Public;
using YookingDBNormal.Class;
using YookingFileIO.Class;
using YookingKitBox.Class.Public;
using YookingSvrDBCall.Class.DBLogic;
using YookingSvrDBCall.Class.DBPub;

namespace YookingBizWQW.Class.Admin
{
    class clsTreeDefine
    {
        //注释：王庆文 2020年02月09日===================================================
        //获取YOOKING系统定义的全部菜单业务树数据
        public string Get_MnuTreeDefine_Html(string strB64RequestData)
        {
            string strRtData = "no data";

            //先进行解密，然后在进行中文解码，最后得到明文的请求数据
            string strRequestData = clsHandleData.DeUrlRequest(strB64RequestData);

            #region 将strRequestData包含的各种参数一一解析出来

            string strSelfNodeID = "noid"; //本级节点ID号

            //这些参数用★分隔开，客户端与服务器端要一致
            string[] strArray = strRequestData.Split('★');
            if (strArray.Length >= 1)
            {
                strSelfNodeID = strArray[0];
            }

            #endregion

            string strCondition01 = "1=1"; //动态生成SQL查询条件
            string strCondition02 = "1=1"; //动态生成SQL查询条件

            if (strSelfNodeID != "" && strSelfNodeID != "noid") 
            {
                strCondition01 += " and ParentNodeID='" + strSelfNodeID + "' ";
                strCondition02 += " and t.SelfNodeID='" + strSelfNodeID + "' ";
            }

            #region 生成查询记录的SQL语句

            StringBuilder sbQuerySQL = new StringBuilder();
            sbQuerySQL.AppendLine(" select NODETYPE, ");
            sbQuerySQL.AppendLine(" 	   PARENTNODEID, ");
            sbQuerySQL.AppendLine(" 	   SELFNODEID, ");
            sbQuerySQL.AppendLine(" 	   NODELEVEL, ");
            sbQuerySQL.AppendLine(" 	   NODEORDER, ");
            sbQuerySQL.AppendLine(" 	   NODETEXT, ");
            sbQuerySQL.AppendLine(" 	   TOOLTIP, ");
            sbQuerySQL.AppendLine(" 	   IMAGEURL, ");
            sbQuerySQL.AppendLine(" 	   DESKTOPICOURL, ");
            sbQuerySQL.AppendLine(" 	   MOBILEICOURL, ");
            sbQuerySQL.AppendLine(" 	   NAVIGATEURL, ");
            sbQuerySQL.AppendLine(" 	   TARGETFRAME, ");
            sbQuerySQL.AppendLine(" 	   SYS_RECORDID, ");
            sbQuerySQL.AppendLine(" 	   MAXNODEID, ");
            sbQuerySQL.AppendLine(" 	   MAXNODEORDER, ");
            sbQuerySQL.AppendLine(" 	   CHILDMAXORDER ");
            sbQuerySQL.AppendLine("   from MNU_TREEDEFINE t ");
            sbQuerySQL.AppendLine("   left join (select max(SELFNODEID) as MAXNODEID, ");
            sbQuerySQL.AppendLine(" 					max(NODEORDER) as MAXNODEORDER ");
            sbQuerySQL.AppendLine(" 			   from MNU_TREEDEFINE ");
            sbQuerySQL.AppendLine(" 			  where " + clsSQLSyntax.IsNullSQL("Sys", "SYS_CANCELDATETIME"));
            sbQuerySQL.AppendLine(" 				and Sys_InputDateTime >= '2019-12-01 00:00:00') a ");
            sbQuerySQL.AppendLine(" 	on 1=1 ");
            sbQuerySQL.AppendLine("    left join ( select Max(NODEORDER) as CHILDMAXORDER from MNU_TREEDEFINE ");
            sbQuerySQL.AppendLine("  where Sys_InputDateTime >= '2019-12-01 00:00:00' "); //老架构的菜单不读取
            sbQuerySQL.AppendLine("    and " + strCondition01 + " ) b on 1=1 ");
            sbQuerySQL.AppendLine("  where " + clsSQLSyntax.IsNullSQL("Sys", "t.SYS_CANCELDATETIME"));
            sbQuerySQL.AppendLine("    and Sys_InputDateTime >= '2019-12-01 00:00:00' ");  //老架构的菜单不读取 
            sbQuerySQL.AppendLine("    and " + strCondition02);
            sbQuerySQL.AppendLine("    order by NODEORDER asc ");

            #endregion

            //clsRWFile myLog = new clsRWFile(); //将待执行的SQL语句记录下来
            //myLog.WriteInfoToTxtFile("SysLog", "aa.txt", sbQuerySQL.ToString());

            clsDbSQLLogic myDB = new clsDbSQLLogic(); //实例化数据库操作类
            strRtData = myDB.YookingExecuteJson("Sys", sbQuerySQL.ToString());

            return strRtData; //数据库查询结果
        }

        //注释：王庆文 2021年07月20日===================================================
        //获取YOOKING系统定义的全部VUE类型的菜单业务树数据
        public string Get_MnuTreeDefine_Vue(string strB64RequestData)
        {
            string strRtData = "no data";

            //先进行解密，然后在进行中文解码，最后得到明文的请求数据
            string strRequestData = clsHandleData.DeUrlRequest(strB64RequestData);

            #region 将strRequestData包含的各种参数一一解析出来

            string strSelfNodeID = "noid"; //本级节点ID号

            //这些参数用★分隔开，客户端与服务器端要一致
            string[] strArray = strRequestData.Split('★');
            if (strArray.Length >= 1)
            {
                strSelfNodeID = strArray[0];
            }

            #endregion

            string strCondition01 = "1=1"; //动态生成SQL查询条件
            string strCondition02 = "1=1"; //动态生成SQL查询条件

            if (!string.IsNullOrEmpty(strSelfNodeID) && strSelfNodeID != "noid")
            {
                strCondition01 += " and Parent_ID='" + strSelfNodeID + "' ";
                strCondition02 += " and t.Node_ID='" + strSelfNodeID + "' ";
            }

            #region 生成查询记录的SQL语句

            StringBuilder sbQuerySQL = new StringBuilder();
            sbQuerySQL.AppendLine(" select NODE_TYPE, ");
            sbQuerySQL.AppendLine(" 	   PARENT_ID, ");
            sbQuerySQL.AppendLine(" 	   NODE_ID, ");
            sbQuerySQL.AppendLine(" 	   NODE_LEVEL, ");
            sbQuerySQL.AppendLine(" 	   NODE_ORDER, ");
            sbQuerySQL.AppendLine(" 	   TR_TITLE, ");
            sbQuerySQL.AppendLine(" 	   PAGE_TYPE, ");
            sbQuerySQL.AppendLine(" 	   PAGE_NAME, ");
            sbQuerySQL.AppendLine(" 	   FILE_PATH, ");
            sbQuerySQL.AppendLine(" 	   ROUTE_PATH, ");
            sbQuerySQL.AppendLine(" 	   ICO_TYPE, ");
            sbQuerySQL.AppendLine(" 	   ICO_SRC, ");
            sbQuerySQL.AppendLine(" 	   HTMLPAGE, ");
            sbQuerySQL.AppendLine(" 	   ROOT_DISPLAY, ");
            sbQuerySQL.AppendLine(" 	   SUB_DISPLAY, ");
            sbQuerySQL.AppendLine(" 	   SYS_RECORDID, ");
            sbQuerySQL.AppendLine(" 	   MAXNODEID, ");
            sbQuerySQL.AppendLine(" 	   MAXNODEORDER, ");
            sbQuerySQL.AppendLine(" 	   CHILDMAXORDER ");
            sbQuerySQL.AppendLine("   from VUE_TREEDEFINE t ");
            sbQuerySQL.AppendLine("   left join (select max(NODE_ID) as MAXNODEID, ");
            sbQuerySQL.AppendLine(" 					max(NODE_ORDER) as MAXNODEORDER ");
            sbQuerySQL.AppendLine(" 			   from VUE_TREEDEFINE ");
            sbQuerySQL.AppendLine(" 			  where " + clsSQLSyntax.IsNullSQL("Sys", "SYS_CANCELDATETIME"));
            sbQuerySQL.AppendLine(" 		     ) a ");
            sbQuerySQL.AppendLine(" 	on 1=1 ");
            sbQuerySQL.AppendLine("    left join ( select Max(NODE_ORDER) as CHILDMAXORDER from VUE_TREEDEFINE ");
            sbQuerySQL.AppendLine("  where " + strCondition01 + " ) b on 1=1 "); 
            sbQuerySQL.AppendLine("  where " + clsSQLSyntax.IsNullSQL("Sys", "t.SYS_CANCELDATETIME"));
            sbQuerySQL.AppendLine("    and " + strCondition02);
            sbQuerySQL.AppendLine("    order by NODE_ORDER asc ");

            #endregion

            //clsRWFile myLog = new clsRWFile(); //将待执行的SQL语句记录下来
            //myLog.WriteInfoToTxtFile("SysLog", "aa.txt", sbQuerySQL.ToString());

            clsDbSQLLogic myDB = new clsDbSQLLogic(); //实例化数据库操作类
            strRtData = myDB.YookingExecuteJson("Sys", sbQuerySQL.ToString());

            return strRtData; //数据库查询结果
        }

        //注释：王庆文 2020年02月06日===================================================
        //从后台读取并显示当前节点对应的列表图标Url、桌面图标Url、手机图标Url图标数据
        public string GetTreeNodeUrlIcon(string strB64RequestData)
        {
            string strRtData = "no data";

            //先进行解密，然后在进行中文解码，最后得到明文的请求数据
            string strRequestData = clsHandleData.DeUrlRequest(strB64RequestData);

            #region 将strRequestData包含的各种参数一一解析出来

            string strRecordID = "0000"; //主表记录ID
            string strIcoUrlMD5 = "000"; //图标MD5值

            //这些参数用★分隔开，客户端与服务器端要一致
            string[] strArray = strRequestData.Split('★');
            if (strArray.Length >= 2)
            {
                strRecordID = strArray[0];
                strIcoUrlMD5 = strArray[1];
            }

            #endregion

            #region 生成查询记录的SQL语句

            StringBuilder sbQuerySQL = new StringBuilder();
            sbQuerySQL.AppendLine(" SELECT ");
            sbQuerySQL.AppendLine(" 	IMAGEENTITY ");
            sbQuerySQL.AppendLine(" FROM ");
            sbQuerySQL.AppendLine(" 	MDA_PUBLICIMAGE ");
            sbQuerySQL.AppendLine(" WHERE ");
            sbQuerySQL.AppendLine(" 	" + clsSQLSyntax.IsNullSQL("Data", "SYS_CANCELDATETIME"));
            sbQuerySQL.AppendLine(" AND (upper(MAINTABLEDEFINEID) = '2017090912580420102120119901' ");
            sbQuerySQL.AppendLine("      or upper(MAINTABLEDEFINEID) = 'MNU_TREEDEFINE') ");
            sbQuerySQL.AppendLine(" AND MAINTABLERECORDID = '" + strRecordID + "' ");
            sbQuerySQL.AppendLine(" AND SPECIALRELATENUMBER = '" + strIcoUrlMD5 + "' ");

            #endregion

            clsDbSQLLogic myDB = new clsDbSQLLogic(); //实例化数据库操作类
            strRtData = myDB.YookingExecuteJson("Data", sbQuerySQL.ToString());

            return strRtData; //数据库查询结果
        }

        //注释：王庆文 2020年02月06日=============================================================================
        //保存当前点击的TREE节点的属性信息到后台数据库Mnu_TreeDefine中
        public string Save_MnuTreeDefine_Html(string strB64RequestData)
        {
            string strRtData = "no data";

            //先进行解密，然后在进行中文解码，最后得到明文的请求数据
            string strRequestData = clsHandleData.DeUrlRequest(strB64RequestData);

            #region 将strRequestData包含的各种参数一一解析出来

            string strJsonArray = "[{null}]"; //将要向数据库中写入的真实数据内容
            string strNodeOrder_Bak = "0000"; //节点排序值(不会被修改)
            string strNodeOrder = "00000000"; //节点排序值(有可能被修改)
            string strSys_RecordID = "00000"; //记录ID
            string strUserName = "000000000"; //客户端登录用户名

            //这些参数用★分隔开，客户端与服务器端要一致
            string[] strArray = strRequestData.Split('★');
            if (strArray.Length >= 5)
            {
                strJsonArray = strArray[0];
                strNodeOrder_Bak = strArray[1];
                strNodeOrder = strArray[2];
                strSys_RecordID = strArray[3];
                strUserName = strArray[4];
            }

            #endregion

            clsDbSQLLogic myDB = new clsDbSQLLogic(); //实例化数据库操作类

            //判断该节点是否为最新增加的，或者当前节点的排序值被修改过
            if (strSys_RecordID == "" || strNodeOrder_Bak != strNodeOrder)  
            {
                //开始update数据，对NodeOrder的值进行重新排序。
                //调整数据库中现有TREE的排序值，为新记录空出来一个编号。
                StringBuilder sbUpdateSQL = new StringBuilder();
                sbUpdateSQL.AppendLine(" update MNU_TREEDEFINE set NodeOrder=NodeOrder + 1 ");
                sbUpdateSQL.AppendLine("   where NodeOrder >= '" + strNodeOrder + "' ");
     
                object objUpdateCount = myDB.YookingExecuteNonQuery("Sys", sbUpdateSQL.ToString());
            }

            //为了防止update时，输入框原来有值，但是现在修改为空''时,
            //函数WriteDBNormal_UseSYSCol会自动跳过空值字段的问题，
            //只能先彻底的先删除原记录，使得函数强制insert
            string strDeleteSQL = " DELETE FROM MNU_TREEDEFINE ";
            strDeleteSQL += " WHERE SYS_RECORDID='" + strSys_RecordID + "' ";
            myDB.YookingExecuteNonQuery("Sys", strDeleteSQL);

            //将Json数组转成DataSet
            DataSet dsData = clsJConvert.JArryToDataSet(strJsonArray); 

            //先判断数据是否存在，根据判断结果自动执行insert或者update
            clsWriteDB myWrite = new clsWriteDB(); //实例化数据库操作类  
            strRtData = myWrite.WriteDBNormal_UseSYSCol("Sys", "MNU_TREEDEFINE", "SelfNodeID", strUserName, dsData);

            //clsRWFile myLog = new clsRWFile(); //将待执行的SQL语句记录下来
            //myLog.WriteInfoToTxtFile("SysLog", "aa.txt", strJsonArray);

            return strRtData; //数据库操作的最终结果
        }

        //注释：王庆文 2021年07月20日=============================================================================
        //保存当前点击的TREE节点的属性信息到后台数据库Vue_TreeDefine中
        public string Save_MnuTreeDefine_Vue(string strB64RequestData)
        {
            string strRtData = "no data";

            //先进行解密，然后在进行中文解码，最后得到明文的请求数据
            string strRequestData = clsHandleData.DeUrlRequest(strB64RequestData);

            #region 将strRequestData包含的各种参数一一解析出来

            string strJsonArray = "[{null}]"; //将要向数据库中写入的真实数据内容
            string strNodeOrder_Bak = "0000"; //节点排序值(不会被修改)
            string strNodeOrder = "00000000"; //节点排序值(有可能被修改)
            string strSys_RecordID = "00000"; //记录ID
            string strUserName = "000000000"; //客户端登录用户名

            //这些参数用★分隔开，客户端与服务器端要一致
            string[] strArray = strRequestData.Split('★');
            if (strArray.Length >= 5)
            {
                strJsonArray = strArray[0];
                strNodeOrder_Bak = strArray[1];
                strNodeOrder = strArray[2];
                strSys_RecordID = strArray[3];
                strUserName = strArray[4];
            }

            #endregion
     
            clsDbSQLLogic myDB = new clsDbSQLLogic(); //实例化数据库操作类
            
            //判断该节点是否为最新增加的，或者当前节点的排序值被修改过
            if (strSys_RecordID == "" || strNodeOrder_Bak != strNodeOrder)
            {
                //开始update数据，对NodeOrder的值进行重新排序。
                //调整数据库中现有TREE的排序值，为新记录空出来一个编号。
                StringBuilder sbUpdateSQL = new StringBuilder();
                sbUpdateSQL.AppendLine(" update VUE_TREEDEFINE set Node_Order=Node_Order + 1 ");
                sbUpdateSQL.AppendLine("   where Node_Order >= '" + strNodeOrder + "' ");
          
                object objUpdateCount = myDB.YookingExecuteNonQuery("Sys", sbUpdateSQL.ToString());
            }

            //为了防止update时，输入框原来有值，但是现在修改为空''时,
            //函数WriteDBNormal_UseSYSCol会自动跳过空值字段的问题，
            //只能先彻底的先删除原记录，使得函数强制insert
            string strDeleteSQL = " DELETE FROM VUE_TREEDEFINE ";
            strDeleteSQL += " WHERE SYS_RECORDID='" + strSys_RecordID + "' ";
            myDB.YookingExecuteNonQuery("Sys", strDeleteSQL); 

            //将Json数组转成DataSet
            DataSet dsData = clsJConvert.JArryToDataSet(strJsonArray);

            //先判断数据是否存在，根据判断结果自动执行insert或者update
            clsWriteDB myWrite = new clsWriteDB(); //实例化数据库操作类  
            strRtData = myWrite.WriteDBNormal_UseSYSCol("Sys", "VUE_TREEDEFINE", "NODE_ID", strUserName, dsData);

            //clsRWFile myLog = new clsRWFile(); //将待执行的SQL语句记录下来
            //myLog.WriteInfoToTxtFile("SysLog", "aa.txt", strJsonArray);

            return strRtData; //数据库操作的最终结果
        }


    }
}
